package com.manager.core.module.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.manager.commons.annotation.Module;
import com.manager.commons.annotation.Operation;
import com.manager.commons.base.DataGrid;
import com.manager.core.module.log.aop.TMLog;
import com.manager.core.module.manager.entity.article.GeneralTopicCategory;
import com.manager.core.module.manager.service.GeneralTopicCategoryService;
import com.manager.core.web.json.AjaxJson;
import com.manager.gen.web.controller.AdminController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 *
 * @author H
 * @version 1.0
 */
@Controller
@Module(parent = "article-manager", title = "新闻类型", show = true,code = "/gen/article/category")
public class GeneralTopicCategoryController extends AdminController<GeneralTopicCategory> {
    private final String baseUrl = "/gen/article/category";
    @Autowired
    private GeneralTopicCategoryService _service;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(baseUrl+"/index")
    @TMLog(type = "mvp", description = "新闻类型页面")
    public ModelAndView index() {
        return getModelAndView(baseUrl + "/category-index");
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(baseUrl+"/add")
    @Operation(title = "新增")
    @TMLog(type = "mvp", description = "新闻类型-新增")
    public ModelAndView add() {
        return getModelAndView(baseUrl + "/category-edit", getModel());
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(baseUrl+"/edit")
    @Operation(title = "编辑")
    @TMLog(type = "mvp", description = "新闻类型-编辑")
    public ModelAndView edit() {
        return getModelAndView(baseUrl + "/category-edit", _service.findById(getIdParam()));
    }

    /**
     * 分页
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/list", method = RequestMethod.POST)
    @Operation(title = "列表")
    @TMLog(type = "mvp", description = "新闻类型-列表")
    public @ResponseBody DataGrid list() {
        return _service.queryPage(getParams());
    }

    /**
     * 保存数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/save", method = RequestMethod.POST)
    @TMLog(type = "mvp", description = "新闻类型-保存")
    public @ResponseBody AjaxJson save() {
        return _service.save(getParams());
    }

    /**
     * 改变数据状态
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/status", method = RequestMethod.POST)
    @Operation(title = "修改状态")
    @TMLog(type = "mvp", description = "新闻类型-修改状态")
    public @ResponseBody AjaxJson status() {
        return _service.status(getParams());
    }

    /**
     * 删除数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/delete", method = RequestMethod.POST)
    @Operation(title = "删除")
    @TMLog(type = "mvp", description = "新闻类型-删除")
    public @ResponseBody AjaxJson delete() {
        return _service.delete(getParams());
    }

    /**
     * 销毁数据
     *
     * @return
     */
    @TMLog(type = "mvp", description = "新闻类型-销毁")
    @RequestMapping(value = baseUrl+"/destroy", method = RequestMethod.POST)
    public @ResponseBody AjaxJson destroy() {
        return _service.destroy(getParams());
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/tree", method = RequestMethod.POST)
    public @ResponseBody JSONArray tree() {
        return JSON.parseArray(JSONArray.toJSONString(_service.queryTreeList()));
    }

    @Override
    public GeneralTopicCategory getModel() {
        return new GeneralTopicCategory();
    }
}